﻿using BZOrderPrint.DAL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BZOrderPrint
{
    public partial class LoginForm : Form
    {
        private BZContext context;
        public LoginForm()
        {
            InitializeComponent();
        }

        private void Init()
        {
            context = new BZContext(ConfigHelper.ConnString);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(userName.Text) || string.IsNullOrEmpty(passWord.Text))
            {
                MessageBox.Show("账户密码不能为空");
                return;
            }


            int count = context.bzUserInfoEntities.Count(x => x.UserName == userName.Text && x.Password == passWord.Text);
            if (count > 0)
            {
                if (checkBox1.Checked == true)
                {
                    ConfigHelper.modifyConfig("userName", userName.Text);
                    ConfigHelper.modifyConfig("password", passWord.Text);
                    ConfigHelper.modifyConfig("remember", "true");
                }
                else {
                    ConfigHelper.modifyConfig("userName", "");
                    ConfigHelper.modifyConfig("password", "");
                    ConfigHelper.modifyConfig("remember", "false");

                }
                this.Hide();
                MainForm main = new MainForm();
                main.ShowDialog();
                this.Close();
                return;
            }
            else
            {
                MessageBox.Show("账户或密码错误");
                return;
            }

        }

        private void LoginForm_Load(object sender, EventArgs e)
        {
            Init();
            string remember = ConfigHelper.GetAppSettingValue("remember");
            if (remember == "true")
            {
                userName.Text = ConfigHelper.GetAppSettingValue("userName");
                passWord.Text = ConfigHelper.GetAppSettingValue("password");
            }

        }
    }
}
